package com.etool.modules.auth.service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

import com.cache.simplecache.SimpleCacheManager;
import com.etool.commons.dao.DaoBase;
import com.etool.commons.sql.SearchCondition;
import com.etool.modules.auth.model.AuthMenu;

@Component
public class AuthMenuService extends DaoBase<AuthMenu>{
	
	public Map<String,AuthMenu> cache=new HashMap<String, AuthMenu>();
	
	
	public AuthMenuService()
	{
		tableName="auth_worker";
	}
		
	@Autowired
	public void setSimpleCacheManager(@Qualifier("simpleCacheManager")SimpleCacheManager simpleCacheManager) {
		this.simpleCacheManager=simpleCacheManager;
	}

	@Autowired
	public void setJdbc(@Qualifier("jdbc")NamedParameterJdbcTemplate jdbc) {
		this.jdbc=jdbc;
	}
	
	public AuthMenu cacheSelectById(int menuId)
	{
		String key="auth_menu:{menuId:"+menuId+"}";
		
		AuthMenu menu=null;
		
		if(cache.get(key)==null)
		{
			List<AuthMenu> menus=select(Collections.singletonList((new SearchCondition("menuId", menuId))));
		
			if(menus!=null && menus.size()==1)
			{
				menu=menus.get(0);
				cache.put(key, menu);
			}
		}
		else
		{
			menu=cache.get(key);
		}
		
		return menu;
	}

}
